# !/usr/bin/env python
# -*- coding:utf-8 -*-
# @FileName : p20.py
# @Time     : 2024/5/10 23:25
# @Author   : Robot-Zsj
"""
description:

执行任务赚积分

https://fcqian.blog.csdn.net/article/details/134719131

i:
4
3
1 2
1 3
1 4
1 5

o:
5
"""
import queue

n = int(input())
t = int(input())
wos = [list(map(int, input().split())) for i in range(n)]


def getResult():
    wos.sort(key=lambda x: x[0])
    pq = queue.PriorityQueue()
    ans = 0
    curTime = 0

    for wo in wos:
        endTime, score = wo
        if curTime < endTime:
            pq.put(score)
            ans += score
            curTime += 1
        else:
            if pq.qsize() == 0:
                continue
            min_score = pq.queue[0]

            if score > min_score:
                pq.get()
                pq.put(score)
                ans += score - min_score

    while pq.qsize() > t:
        ans -= pq.get()

    return ans


print(getResult())
